package com.xiaotu.principle.inversion.improve;

/**
 * @author 张晓2222
 * @create 2020-06-17 17:39
 */
public class DependencyPass {

    public static void main(String[] args) {

//        //方式1：接口传递
//        OpenAndClose openAndClose = new OpenAndClose();
//        openAndClose.open(new ChangHong());
//        //方式2：构造方法传递
//        OpenAndClose openAndClose = new OpenAndClose(new XiaoMi());
//        openAndClose.open();
//        //方式3：setter方法传递
//        OpenAndClose openAndClose = new OpenAndClose();
//        openAndClose.setTv(new HuaWei());
//        openAndClose.open();
    }

}

////方式1：通过接口传递实现依赖
////开关的接口
//interface IOpenAndClose{
//    public void open(ITV tv);
//}
////ITV接口
//interface ITV{
//    public void play();
//}
//
//class ChangHong implements ITV{
//
//    @Override
//    public void play() {
//        System.out.println("长虹电视机，打开");
//    }
//}
//
//
//class OpenAndClose implements IOpenAndClose{
//
//    @Override
//    public void open(ITV tv) {
//        tv.play();
//    }
//}


////方式2：通过构造方法依赖传递
//interface IOpenAndClose{
//    public void open();//抽象方法
//}
//interface ITV{//ITV接口
//    public void play();
//}
//
//class XiaoMi implements ITV{
//
//    @Override
//    public void play() {
//        System.out.println("小米电视，打开");
//    }
//}
//
//class OpenAndClose implements IOpenAndClose{
//
//    public ITV tv;
//
//    public OpenAndClose(ITV tv){//构造器
//        this.tv = tv;
//    }
//
//    @Override
//    public void open() {
//        tv.play();
//    }
//}

//方式3，通过setter方法传递
interface IOpenAndClose{

    public void open();

    public void setTv(ITV tv);

}


interface ITV{//ITV接口
    public void play();
}


class HuaWei implements ITV{

    @Override
    public void play() {
        System.out.println("华为电视，打开");
    }
}

class OpenAndClose implements IOpenAndClose{

    private ITV tv;

    @Override
    public void open() {
        this.tv.play();
    }

    @Override
    public void setTv(ITV tv) {
        this.tv = tv;
    }
}
